﻿using System;
using System.Configuration;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using AMM.Business.Config;
using AMM.CustomClassLibrary;
using AMM.Model;

namespace AMM.Forms {
    public partial class FrmLogin : FrmRootBase {

        protected override void Button_Click(object sender, EventArgs e) {
            Button btn = sender as Button;
            switch (btn.Name) {
                case "btnLogin": this.Login(); break;
                case "btnClose": this.Close(); break;
            }
        }

        public FrmLogin() {
            this.InitializeComponent();
        }

        private void Login() {
            User user = null;
            string name = this.tbLoginName.Text;
            string pwd = this.tbLoginPassword.Text;
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd)) {
                this.ShowError("登录名称或登录密码为空！");
                return;
            }

            if (name.ToLower() == "maintain" && pwd.ToLower() == "amm#2014") {
                string xmlFileName = Path.Combine(Application.StartupPath, "Maintain.xml");
                if (File.Exists(xmlFileName)) {
                    XmlDocument xd = new XmlDocument();
                    xd.Load(xmlFileName);
                    string connectionString = xd.DocumentElement.SelectSingleNode("descendant::connectionstring").InnerText;

                    System.Configuration.Configuration configuration =
                        ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                    configuration.AppSettings.Settings["AMMConnectionString"].Value = SecurityHelper.DESEncrypt(connectionString);
                    configuration.Save();

                    this.ShowInfo("更新完成！");
                    this.tbLoginName.Clear();
                    this.tbLoginPassword.Clear();
                    return;
                }
            }

            this.PreventCancel = true;
            this.btnLogin.Enabled = this.btnClose.Enabled = false;
            this.ExitApplicaton = false;
            this.Text = "登录中...";

            this.CreateNewThread(() => {
                user = new UserProcessor().UserLogin(name, pwd);
            }, () => {
                this.btnLogin.Enabled = this.btnClose.Enabled = true;
                this.Text = "登录";
                this.PreventCancel = false;

                if (user == null) {
                    this.ExitApplicaton = true;
                    this.ShowError("错误的用户名或密码！");
                    return;
                }

                LoginUserId = user.UserId;
                LoginUserName = user.Name;
                IsSystem = user.IsSystem;
                IsOffline = bool.Parse(new ConfigurationProcessor().FetchConfigurationByKeyCode("Offline").KeyValue);

                this.Hide();
                FrmEntry.LoadAndRun(new FrmMDIContainer());
            });
        }

        private void FrmLogin_Load(object sender, EventArgs e) {
            this.ExitApplicaton = true;
            this.Text = "登录";

            this.tbLoginName.Text = "admin";
            this.tbLoginPassword.Text = "123456";
        }
    }
}
